Although you will be learning R in this class, it might be more appropriate to say that you are learning the tidyverse.
The tidyverse is a set of packages that share an underlying design philosophy, grammar, and data structures. The tidyverse consists of packages that are simple and intuitive to use and will take you from importing data (with readr), restructuring and transforming data (with tidyr and dplyr), and to graphically visualizing data (with ggplot2).
The language of the dplyr package will be the underlying framework for how you will think about manipulating and transforming data in R.
dplyr uses intuitive language that you are already familiar with.
rename() renames columns
filter() filters rows based on their values in specified columns
select() selects (or removes) columns
mutate() creates new columns based on transformation from other columns, or edits values within existing columns
summarise() aggregates across rows to create a summary statistic (means, standard deviations, etc.)
These data come from a hypothetical (I made it up) research study to compare the effectiveness of two memory techniques, a mnemonic technique and a spaced repetition technique, for improving memory retention. Participants were randomly assigned to one of the two memory techniques and completed 3 memory tests (A, B, and C). The number of correctly recalled words for each memory test was recorded in the two data files by research assistants.
Example Data Set
Use what you learned from Class 1 to explore the data
what are the column names?
what type of values are in each column?
It turns out that the research assistant who ran participants in the spaced repetition condition did not follow the lab’s protocol for recording data 🤦♀️
They:
used wrong column names,
recorded the memory tests as X, Y, and Z (A, B, and C, respectively),
left out what condition these data were from
gave some particpants less than 3 memory tests! 🤬
Rename columns: readr()
rename()
First, let’s fix the RA’s mistake by renaming the columns in the spaced repetition data as they are named in the mnemonic data. We can do so using the rename() function. The format for this function looks something like:
rename(new_name = old_name)
Here is how we would rename the columns in the spaced repetition data we imported.
Let’s remove rows that correspond to those participants that did not complete 3 memory tests. It turns out that those participants were always ran on Thursday or Friday, must have been a bad day for the research assistant 😢.
We can use filter() to remove rows that have Thursday or Friday in the day column.
repetition_data <- repetition_data |>filter(day !="Thursday", day !="Friday")
Select columns: select()
select()
select() allows you to select which columns to keep and/or remove.
select(columns, to, keep)
select(-columns, -to, -remove)
select() can be used with more complex operators and tidyselect functions, see the documentation here.
select()
For the repetition data, let’s only keep the following columns
mutate() is a very powerful function. It basically allows you to do any computation or transformation on the values in the data frame. See the full documentation here.
Within mutate() the = sign functions similarly to the assignment operator <-, where the result of whatever is on the right-hand side of = gets assigned to the column that is specified on the left-hand side (an existing column or a new one you are creating).
mutate()
Add a new column
We need to create a column specifying what condition the spaced repetition data came from, dang RA!
case_when() is basically a sequence of if else type of statements where each statement is evaluated, if it is true then it is given a certain value, else the next statement is evaluated, and so on.
The basic format of case_when() looks like:
mutate(a_column =case_when(a logical statement ~ a value, another statement ~ another value,.default = and another value))
case_when()
Let’s see an example of this with the spaced repetition data. We need to change the values in the word_list column so that X is A, Y is B, and Z is C.
rowwise() is used when you want to perform operations row by row, treating each row as a single group. This is useful when you want to aggregate data (e.g., mean()) across multiple columns.
The data set we are working with does not provide a good demonstration of this so let’s create a different set of data to look at how to use rowwise()
The thing is, we don’t really care about performance on each individual word_list (A, B, and C). We care about the participant’s overall performance, aggregated across all three word lists. To aggregrate data using dplyr we can use summarise().
The result of summarise() is a reduced data frame with fewer rows.
The code inside of summarise() looks a lot like the code we could put in mutate().
The difference is that mutate() does not collapse the data frame but summarise() does.
summarise()
Let’s calculate the mean recall performance by condition and participant. This will result in one row per participant (because it is a between-subject design).
For this activity we will work with a real data set from a paper published in Psychological Science (one of the leading journals in psychology).
Dawtry, R. J., Sutton, R. M., & Sibley, C. G. (2015). Why Wealthier People Think People Are Wealthier, and Why It Matters: From Social Sampling to Attitudes to Redistribution. Psychological Science, 26(9), 1389–1400. https://doi.org/10.1177/0956797615586560
In this research, Dawtry, Sutton, and Sibley (2015) wanted to examine why people differ in their assessments of the increasing wealth inequality within developed nations. Previous research reveals that most people desire a society in which the overall level of wealth is high and that wealth is spread somewhat equally across society. However, support for this approach to income distribution changes across the social strata. In particular, wealthy people tend to view society as already wealthy and thus are satisfied with the status quo, and less likely to support redistribution. In their paper Dawtry et al., (2015) sought to examine why this is the case. The authors propose that one reason wealthy people tend to view the current system is fair is because their social-circle is comprised of other wealthy people, which biases their perceptions of wealth, which leads them to overestimate the mean level of wealth across society.
Class 3 Activity
To test this hypothesis, the authors conducted a study with 305 participants, recruited from an online participant pool. Participants reported their own annual household income, the income level of those within their own social circle, and the income for the entire population. Participants also rated their perception of the level of equality/inequality across their social circle and across society, their level of satisfaction with and perceived fairness of the current system, their attitudes toward redistribution of wealth (measured using a four-item scale), and their political preference.
Key variables we will look at:
Level of satisfactioin with current system (1 = extremely satisfied, 9 = extremely dissatisfied)
Perceived fairness of current system (1 = extremely fair, 9 = extremely unfair)
Attitude on redistribution of wealth (1 = strongly disagree, 6 = strongly agree)
contained in four columms: redist1 through redist4
Political preference (1 = very liberal/very left-wing/strong Democrat, 9 = very conservative/very right-wing/strong Republican): Political_Preference
Setup
Create a new R script and save it as class_3_activity_firstlastname.R
Load the following packages at the top of your script
readr, dplyr, gt, ggplot2
Import the data file
Take some time to explore the data.
What are the column names?
What type of values are in the columns?
How many participants are in the study?
hint: use a combination of length() and unique()
length(unique(data$columnname))
Rename and Filter
Rename the level of satisfaction and perceived fairness columns
In the previous step, you should have noticed how these column names are not ideal. They contain spaces and even a special character ? . You will need to use the special quotation mark ` `to reference these column names in rename() , e.g., `column name with spaces` You can find these special quotation marks to the left of the 1 key and above the tab key.
Filter by only keeping rows in which Political_Preference is not missing NA . Note how many fewer rows there are in the data after filtering.
hint: use filter(!is.na()) to evaluate whether values in Political_Preference are NOT ! missing is.na()
Show cheat code
filter(!is.na(Political_Preference))
Select and Transform
Select only the columns that contain the key variables we are interested in.
Reverse score redist2 and redist4, so that 6=1, 5=2, 4=3, 3=4, 2=5, 1=6.
Use mutate() and case_when()
Aggregate values across rows
Calculate a single variable representing participant’s mean attitude on redistrubtion of wealth
Calculate a single variable representing participant’s mean perception that the current systen is satisfactory and fair.
Use a combination of rowwise() and mutate()
Summarize
Create a new data frame summarizing the values for attitude on redistribution and the combined satisfactory and fairness variable for each level of political preference. (calculate the mean when summarizing)
Use summarise(.by = )
Create a table of this summarized data frame
Use gt() from the gt package, e.g.,
gt(new_data)
Plot
Create a line plot of this summarized data frame
Copy and paste code on next slide
Plot
You will need to change the name of variables to match how you created them.
data_summary, redist_mean, and fairnesss_satisfactory_mean